/*   1:    */ package com.atomikos.icatch.imp;
/*   2:    */ 
/*   3:    */ import com.atomikos.diagnostics.Console;
/*   4:    */ import com.atomikos.finitestates.FSMEnterEvent;
/*   5:    */ import com.atomikos.finitestates.FSMEnterListener;
/*   6:    */ import com.atomikos.icatch.CompositeCoordinator;
/*   7:    */ import com.atomikos.icatch.CompositeTerminator;
/*   8:    */ import com.atomikos.icatch.CompositeTransaction;
/*   9:    */ import com.atomikos.icatch.Extent;
/*  10:    */ import com.atomikos.icatch.HeurHazardException;
/*  11:    */ import com.atomikos.icatch.HeurMixedException;
/*  12:    */ import com.atomikos.icatch.HeurRollbackException;
/*  13:    */ import com.atomikos.icatch.Participant;
/*  14:    */ import com.atomikos.icatch.RecoveryCoordinator;
/*  15:    */ import com.atomikos.icatch.RollbackException;
/*  16:    */ import com.atomikos.icatch.SubTxAwareParticipant;
/*  17:    */ import com.atomikos.icatch.Synchronization;
/*  18:    */ import com.atomikos.icatch.SysException;
/*  19:    */ import com.atomikos.icatch.TransactionControl;
/*  20:    */ import com.atomikos.icatch.TxState;
/*  21:    */ import com.atomikos.icatch.system.Configuration;
/*  22:    */ import java.util.Stack;
/*  23:    */ 
/*  24:    */ public class CompositeTransactionImp
/*  25:    */   extends AbstractCompositeTransaction
/*  26:    */   implements TransactionControl, FSMEnterListener
/*  27:    */ {
/*  28: 62 */   protected CoordinatorImp coordinator_ = null;
/*  29:    */   protected TransactionServiceImp txservice_;
/*  30: 68 */   protected Extent extent_ = null;
/*  31:    */   protected boolean localRoot_;
/*  32:    */   private TransactionStateHandler stateHandler_;
/*  33:    */   
/*  34:    */   CompositeTransactionImp(Stack lineage, String tid, boolean serial, CoordinatorImp coordinator)
/*  35:    */   {
/*  36: 83 */     this(null, lineage, tid, serial, coordinator);
/*  37:    */   }
/*  38:    */   
/*  39:    */   public CompositeTransactionImp(TransactionServiceImp txservice, Stack lineage, String tid, boolean serial, CoordinatorImp coordinator)
/*  40:    */     throws IllegalStateException
/*  41:    */   {
/*  42:108 */     super(tid, lineage, serial);
/*  43:109 */     this.coordinator_ = coordinator;
/*  44:110 */     this.txservice_ = txservice;
/*  45:    */     
/*  46:    */ 
/*  47:    */ 
/*  48:114 */     this.extent_ = null;
/*  49:    */     
/*  50:    */ 
/*  51:117 */     this.localRoot_ = true;
/*  52:118 */     this.stateHandler_ = new TxActiveStateHandler(this);
/*  53:119 */     coordinator.addFSMEnterListener(this, TxState.TERMINATED);
/*  54:    */   }
/*  55:    */   
/*  56:    */   synchronized void localSetTransactionStateHandler(TransactionStateHandler handler)
/*  57:    */   {
/*  58:125 */     this.stateHandler_ = handler;
/*  59:    */   }
/*  60:    */   
/*  61:    */   synchronized void localTestAndSetTransactionStateHandler(TransactionStateHandler expected, TransactionStateHandler newHandler)
/*  62:    */   {
/*  63:130 */     if (this.stateHandler_ != expected) {
/*  64:130 */       throw new IllegalStateException("State is no longer " + expected.getState() + " but " + newHandler.getState());
/*  65:    */     }
/*  66:131 */     localSetTransactionStateHandler(newHandler);
/*  67:    */   }
/*  68:    */   
/*  69:    */   synchronized TransactionStateHandler localGetTransactionStateHandler()
/*  70:    */   {
/*  71:136 */     return this.stateHandler_;
/*  72:    */   }
/*  73:    */   
/*  74:    */   boolean isLocalRoot()
/*  75:    */   {
/*  76:141 */     return this.localRoot_;
/*  77:    */   }
/*  78:    */   
/*  79:    */   TransactionServiceImp getTransactionService()
/*  80:    */   {
/*  81:146 */     return this.txservice_;
/*  82:    */   }
/*  83:    */   
/*  84:    */   CoordinatorImp getCoordinatorImp()
/*  85:    */   {
/*  86:151 */     return this.coordinator_;
/*  87:    */   }
/*  88:    */   
/*  89:    */   private void printMsg(String msg, int level)
/*  90:    */   {
/*  91:    */     try
/*  92:    */     {
/*  93:163 */       Console console = Configuration.getConsole();
/*  94:164 */       if (console != null) {
/*  95:165 */         console.println(msg, level);
/*  96:    */       }
/*  97:    */     }
/*  98:    */     catch (Exception ignore) {}
/*  99:    */   }
/* 100:    */   
/* 101:    */   public TransactionControl getTransactionControl()
/* 102:    */   {
/* 103:177 */     return this;
/* 104:    */   }
/* 105:    */   
/* 106:    */   public int getLocalSubTxCount()
/* 107:    */   {
/* 108:186 */     return localGetTransactionStateHandler().getSubTransactionCount();
/* 109:    */   }
/* 110:    */   
/* 111:    */   public synchronized void setSerial()
/* 112:    */     throws IllegalStateException, SysException
/* 113:    */   {
/* 114:196 */     if (!isRoot()) {
/* 115:197 */       throw new IllegalStateException("setSerial() not allowed: not root tx.");
/* 116:    */     }
/* 117:202 */     this.serial_ = true;
/* 118:    */   }
/* 119:    */   
/* 120:    */   public CompositeTransaction createSubTransaction()
/* 121:    */     throws SysException, IllegalStateException
/* 122:    */   {
/* 123:219 */     CompositeTransaction ret = localGetTransactionStateHandler().createSubTransaction();
/* 124:220 */     printMsg("createSubTransaction(): created new SUBTRANSACTION " + ret.getTid() + " for existing transaction " + getTid(), 2);
/* 125:    */     
/* 126:    */ 
/* 127:223 */     return ret;
/* 128:    */   }
/* 129:    */   
/* 130:    */   public RecoveryCoordinator addParticipant(Participant participant)
/* 131:    */     throws SysException, IllegalStateException
/* 132:    */   {
/* 133:235 */     RecoveryCoordinator ret = localGetTransactionStateHandler().addParticipant(participant);
/* 134:236 */     printMsg("addParticipant ( " + participant + " ) for transaction " + getTid(), 2);
/* 135:    */     
/* 136:238 */     return ret;
/* 137:    */   }
/* 138:    */   
/* 139:    */   public void registerSynchronization(Synchronization sync)
/* 140:    */     throws IllegalStateException, UnsupportedOperationException, SysException
/* 141:    */   {
/* 142:248 */     localGetTransactionStateHandler().registerSynchronization(sync);
/* 143:249 */     printMsg("registerSynchronization ( " + sync + " ) for transaction " + getTid(), 2);
/* 144:    */   }
/* 145:    */   
/* 146:    */   public void addSubTxAwareParticipant(SubTxAwareParticipant subtxaware)
/* 147:    */     throws SysException, IllegalStateException
/* 148:    */   {
/* 149:260 */     localGetTransactionStateHandler().addSubTxAwareParticipant(subtxaware);
/* 150:    */   }
/* 151:    */   
/* 152:    */   protected void doRollback()
/* 153:    */     throws IllegalStateException, SysException
/* 154:    */   {
/* 155:271 */     localGetTransactionStateHandler().rollbackWithStateCheck();
/* 156:272 */     printMsg("rollback() done of transaction " + getTid(), 2);
/* 157:    */   }
/* 158:    */   
/* 159:    */   public CompositeCoordinator getCompositeCoordinator()
/* 160:    */     throws SysException
/* 161:    */   {
/* 162:282 */     return this.coordinator_;
/* 163:    */   }
/* 164:    */   
/* 165:    */   public boolean isLocal()
/* 166:    */   {
/* 167:291 */     return true;
/* 168:    */   }
/* 169:    */   
/* 170:    */   public CompositeTerminator getTerminator()
/* 171:    */   {
/* 172:300 */     return new CompositeTerminatorImp(this.txservice_, this, this.coordinator_);
/* 173:    */   }
/* 174:    */   
/* 175:    */   protected void doCommit()
/* 176:    */     throws SysException, IllegalStateException, RollbackException
/* 177:    */   {
/* 178:319 */     localGetTransactionStateHandler().commit();
/* 179:320 */     printMsg("commit() done (by application) of transaction " + getTid(), 2);
/* 180:    */   }
/* 181:    */   
/* 182:    */   public long getTimeout()
/* 183:    */   {
/* 184:332 */     return this.coordinator_.getTimeOut();
/* 185:    */   }
/* 186:    */   
/* 187:    */   public synchronized Extent getExtent()
/* 188:    */   {
/* 189:341 */     if (this.extent_ == null) {
/* 190:342 */       this.extent_ = new ExtentImp();
/* 191:    */     }
/* 192:343 */     return this.extent_;
/* 193:    */   }
/* 194:    */   
/* 195:    */   public void setRollbackOnly()
/* 196:    */   {
/* 197:356 */     localGetTransactionStateHandler().setRollbackOnly();
/* 198:357 */     printMsg("setRollbackOnly() called for transaction " + getTid(), 2);
/* 199:    */   }
/* 200:    */   
/* 201:    */   public void commit()
/* 202:    */     throws HeurRollbackException, HeurMixedException, HeurHazardException, SysException, SecurityException, RollbackException
/* 203:    */   {
/* 204:369 */     getTerminator().commit();
/* 205:    */   }
/* 206:    */   
/* 207:    */   public void rollback()
/* 208:    */     throws IllegalStateException, SysException
/* 209:    */   {
/* 210:380 */     getTerminator().rollback();
/* 211:    */   }
/* 212:    */   
/* 213:    */   public Object getState()
/* 214:    */   {
/* 215:398 */     return localGetTransactionStateHandler().getState();
/* 216:    */   }
/* 217:    */   
/* 218:    */   public void entered(FSMEnterEvent event)
/* 219:    */   {
/* 220:410 */     if ((getState().equals(TxState.ACTIVE)) || (getState().equals(TxState.MARKED_ABORT))) {
/* 221:    */       try
/* 222:    */       {
/* 223:413 */         boolean recoverableWhileActive = false;
/* 224:414 */         Boolean pref = this.coordinator_.isRecoverableWhileActive();
/* 225:415 */         if (pref != null) {
/* 226:415 */           recoverableWhileActive = pref.booleanValue();
/* 227:    */         }
/* 228:416 */         if ((!recoverableWhileActive) && (!(this.stateHandler_ instanceof TxTerminatedStateHandler))) {
/* 229:419 */           setRollbackOnly();
/* 230:    */         } else {
/* 231:421 */           rollback();
/* 232:    */         }
/* 233:    */       }
/* 234:    */       catch (Exception e) {}
/* 235:    */     }
/* 236:    */   }
/* 237:    */ }


/* Location:           C:\myfile\mysoft\apache-maven-3.5.0-bin\apache-maven-3.5.0\myrep\com\atomikos\transactions\3.7.0\transactions-3.7.0.jar
 * Qualified Name:     com.atomikos.icatch.imp.CompositeTransactionImp
 * JD-Core Version:    0.7.0.1
 */